<template>
  <div class="login-container">
    <div class="login-div">
      <div class="login-title">
        <h3>{{appName}}</h3>
      </div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="ruleForm">
        <el-form-item label prop="username">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label prop="password">
          <el-input
            prefix-icon="el-icon-key"
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" style="width:100%">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { getUserInfo } from "@/api/user";

export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输用户名"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("password");
        }
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 6) {
        callback(new Error("密码至少为6位"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }]
      }
    };
  },
  async mounted() {
    const { data: res } = await this.$http.get("users");
    this.ruleForm.username = this.$store.state.user.username;
    this.ruleForm.password = this.$store.state.user.password;
    //使用promise对象模拟的接口userInfo
    try {
      const {
        data: { username }
      } = await getUserInfo();
      console.log(username);
    } catch (err) {
      console.log(err);
    }
  },
  computed: {
    ...mapState({
      appName: state => state.appName, //这个state相当于根的state
      username: state => state.user.username
    })
    // ...mapState(["appName", "username", "password"])
    // appName() {
    //   return this.$store.state.appName;
    // },
    // username() {
    //   return this.$store.state.user.username;
    // }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$message.success("登录成功");
          sessionStorage.setItem("login", "yes");
          this.$router.push("/");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
el-button {
  width: 100%;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  background-color: #2b4b6b;
}

.login-title {
  margin-bottom: 20px;
  font-size: 30px;
  color: #2b4b6b;
}

.login-div {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 400px;
  height: 350px;
  background-color: white;
  box-shadow: 0 0 20px rgb(34, 16, 16);
  border-radius: 6px;
}
.ruleForm {
  width: 90%;
}
</style>